import { getToken, setToken, removeToken } from '@/utils/auth'
import { login, getInfo, getInfoDetail } from '@/api/user'
import { resetRouter } from '@/router'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    userInfo: {}
  },
  mutations: {
    setToken(state, payload) {
      state.token = payload
      setToken(payload)
    },
    delToken(state) {
      state.token = ''
      removeToken()
    },
    setUserInfo(state, payload) {
      state.userInfo = payload
    },
    delUserInfo(state) {
      state.userInfo = {}
    }

  },
  actions: {
    async userLogin(context, payload) {
      const { data } = await login(payload)
      context.commit('setToken', data)
    },

    async userInfo(context, payload) {
      const info = await getInfo()
      const res = await getInfoDetail(info.data.userId)
      context.commit('setUserInfo', { ...info.data, ...res.data })
      return info.data.roles.menus
    },

    logout(context) {
      context.commit('delToken')
      context.commit('delUserInfo')
      // 3. 重置路由
      resetRouter()
    }
  }
}

